// pages/inviation/createPic.js
const app = getApp();
Page({

     /**
      * 页面的初始数据
      */
     data: {
          width:'', //canvas 宽度
          height:'',  //canvas 高度
          userInfo: {}
     },

     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function (options) {
          let that = this;
          that.setData({
               width: wx.getSystemInfoSync().screenWidth * 0.9   ,
               height: wx.getSystemInfoSync().screenWidth * 0.9 * 1.42
          });
          app
          //登录
          app.login(function (userInfo) {
               that.setData({
                    userInfo: userInfo.data
               });
               //生产用户自己独一无二的分享小程序码
               wx.getImageInfo({
                    src: app.globalData.siteroot + '/upload/inviqrcode/' + userInfo.data.uid + '.png',
                    complete: function (pic) {
                         if(pic.path == undefined) {
                              app.connect('account/getInviQrcode',{
                                   uid:that.data.userInfo.uid,
                                   openid:that.data.userInfo.openId
                              },function(res){
                                   that.onLoad();
                              });
                         }
                         console.log(pic);
                    }
               })
            
               //二维码图
               wx.getImageInfo({
                    src: app.globalData.siteroot + '/upload/inviqrcode/' + userInfo.data.uid + '.png',
                    complete: function (pic) {
                         //生成canvas
                         var ctx = wx.createCanvasContext('canvas');
                         var width = that.data.width;
                         var height = that.data.height;
                         ctx.setFillStyle('#DA4830')
                         //先画底图
                         that.roundRect2(ctx, 0, 0, width, height, 20, true, '#000')
                         ctx.fillRect(0, 0, width, height)
                         ctx.drawImage('../icon/createPic.png', 0, 0, width, height)
                         //画个二维码
                         ctx.drawImage(pic.path, (width - width * 0.32) / 2, height * 0.55, width * 0.32, width * 0.32) 
                         ctx.draw()
                    }
               })

             
          });

     },

     roundRect2: function (ctx, x, y, w, h, r, stroke, color) {
          ctx.save();
          ctx.beginPath()
          if (stroke) {
               ctx.setStrokeStyle(color);
          } else {
               ctx.setFillStyle(color);
          }
          x += 0.5;
          y += 0.5;
          ctx.setGlobalAlpha(0)
          ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);

          ctx.moveTo(x + r, y);

          ctx.lineTo(x + w - r, y);

          ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);

          ctx.lineTo(x + w, y + h - r);

          ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);

          ctx.lineTo(x + r, y + h);

          ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);

          ctx.lineTo(x, y + r);

          if (stroke) {
               ctx.stroke();
          } else {
               ctx.fill();
          };
          ctx.clip();
          ctx.setGlobalAlpha(1)

     },
     saveImg: function () {
          let that = this;
          //保存临时图片
          wx.canvasToTempFilePath({
               x: 0,
               y: 0,
               canvasId: 'canvas',
               quality: 1,
               success: function (res) {
                    // console.log("get tempfilepath(success) is:", res.tempFilePath)
                    //将图片保存在系统相册中(应先获取权限，)
                    wx.saveImageToPhotosAlbum({
                         filePath: res.tempFilePath,
                         success(res) {
                              wx.showToast({
                                   icon: 'success',
                                   title: '图片保存成功',
                              })
                              console.log("save photo is success")
                         },
                         fail: function (e) {
                              wx.showToast({
                                   title: '请授权后再试,如果还不能保存请到设置页打开授权',
                                   icon: 'none'
                              })
                              wx.authorize({
                                   scope: 'scope.writePhotosAlbum',
                                   success() {

                                   }
                              })
                              console.log("save photo is fail")
                         }
                    })
               },
               fail: function (e) {
                    console.log(e)
               }
          })
     },
})